import Vue from "vue"


const config = require("./config");
export default {
  api_url: function (api_name, parm) {
    let _parm = '';
    for (let key in parm) {
      let val = parm[key];
      _parm += key + "=" + val;
    }
    return config.api_website + config.api_model + api_name + '/' + _parm;
  },

  // splitName = MAGAPPX
  userAgent: function () {
    return navigator.userAgent;
  },

  setLocalStrange: function (name, obj) {
    if (typeof obj === "object") {
      localStorage.setItem(name, JSON.stringify(obj));
    } else {
      localStorage.setItem(name, obj);
    }
  },

  getLocalStrange: function (name) {
    let data = localStorage.getItem(name);
    if (typeof data === "string")
      return JSON.parse(data);
    return data;
  },

  emptyObj: function (obj) {
    return JSON.stringify(obj) === "{}" || JSON.stringify(obj) === "null" || JSON.stringify(obj) === "undefind";
  },

  listHas(list, item) {
    return list.indexOf(item) !== -1
  },

  print: function (...args) {
    console.log(args)
  },

  cutStr(str, n) {
    if (str.replace(/[\u4e00-\u9fa5]/g, "**").length <= n) {
      return str;
    }
    else {
      let len = 0;
      let tmpStr = "";
      for (let i = 0; i < str.length; i++) {
        if (/[\u4e00-\u9fa5]/.test(str[i])) {
          len += 2;
        }
        else {
          len += 1;
        }
        if (len > n) {
          break;
        }
        else {
          tmpStr += str[i];
        }
      }
      return tmpStr + " ...";
    }
  },
  min(numberList) {
    if (this.isAllEqual(numberList)) {
      return numberList[0];
    } else {
      return Math.min.apply(null, numberList)
    }
  },
  max(numberList) {
    if (this.isAllEqual(numberList)) {
      return numberList[0];
    } else {
      return Math.max.apply(null, numberList)
    }
  },
  isAllEqual(array) {
    if (array.length > 0) {
      return !array.some(function (value, index) {
        return value !== array[0];
      });
    } else {
      return true;
    }
  }
}
